using B.S.Data.MES.API.Applications.BaseData.Command.ProductMaterialCommand;
using B.S.Domain.BasicData.MaterialManagement.Product_material;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.ProductMaterialCommandHandlers
{
    public class DeleteProductMaterialCommandHandler : IRequestHandler<DeleteProductMaterialCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<Product_material> _productMaterialRepository;

        public DeleteProductMaterialCommandHandler(IBaseRepository<Product_material> productMaterialRepository)
        {
            _productMaterialRepository = productMaterialRepository;
        }

        public async Task<APIRequst<int>> Handle(DeleteProductMaterialCommand request, CancellationToken cancellationToken)
        {
            var existingProductMaterial = _productMaterialRepository.GetAll().FirstOrDefault(x => x.Productid == request.Productid);
            existingProductMaterial.isDel = true;
            await _productMaterialRepository.Update(existingProductMaterial);
            
            return new APIRequst<int>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString()
            };
        }
    }
}